﻿<cfsilent>
	<cfscript>
		
		event.setArg("pageTitle", "教学运行 - 教学场地调度 - 教室借用向导");
		
		mathAdvice = getProperty("serviceFactory").getBean("senateMathAdvice");
		dictAdvice = getProperty("serviceFactory").getBean("senateDictionaryAdvice");

		termId = event.getArg("TermID");
		academicYear = event.getArg("AcademicYear");
		academicTerm = event.getArg("AcademicTerm");
		termDateStart = event.getArg("DateStart");
		
		/* 起始周 */
		w1 = event.getArg("W1");
		/* 结束周 */
		w2 = event.getArg("W2");
		/* 星期几 */
		wkday = event.getArg("WKDAY");
		/* 时段 */
		wkcell = event.getArg("CELL");
		/* 校区 */
		campusId = event.getArg("CAMP");
		/* 教室类别 */
		roomType = event.getArg("CT");
		/* 教室座位 */
		roomSize = event.getArg("SIZE");
		
		/* 时段长度 */
		cells = 2;
		switch ( wkcell ) {
			
			case 'S':  /* 上午 */
				cells = 5; 
				break;
			
			case 'X': /* 下午 */
			case 'W': /* 晚上 */
				cells = 4; 
				break;

			case 'B': /* 白天 */
				cells = 9; 
				break;
			
			case 'Q': /* 全天 */
				cells = 13; 
				break;
		}
		
		/* 转换节次起点 */
		cellStart = mathAdvice.convertLectureIdxToCellStart( wkcell );
		/* 转换节次终点 */
		cellEnd = cellStart + cells -1;
		
		/* 添加楼宇过滤 */
		buildId = event.getArg("BID");
		buildSQL = "";

		if ( len(buildId) ) {
			buildSQL = " AND c.b_id = '" & buildId & "' ";
		}
		
		/* 添加周次过滤 */
		weekSQL = " AND ( ";
		
		for ( w=w1; w lte w2; w++ ) {
			weekSQL = weekSQL & " substr( b.widx, " & w & ", 1 ) = '1' ";
			if ( w lt w2 ) {
				weekSQL = weekSQL & " OR ";
			}
			else {
				weekSQL = weekSQL & " ) ";
			}
		}
		
		/* 查询可用教室 */
		sql = "SELECT 
				cr_id, classroom, sits, sits_course, sits_exam, cr_floor, b_name 
				FROM t_classroom a 
				INNER JOIN t_building c ON c.b_id = a.b_id " & buildSQL & "
				WHERE 
					a.campus_id = :campusId 
					AND 
					a.cr_type = :roomType 
					AND 
					( 
					a.sits > :sitsMin AND a.sits < :sitsMax
					)
					AND 
					substr( a.cr_usage, 4, 1 ) = '1' 
					AND 
					NOT EXISTS (
						SELECT 1 FROM t_clsroom_timetable b
						WHERE 
							a.cr_id = b.cr_id 
							AND 
							b.term_id = :termId 
							AND 
							b.wdidx = :wkday 
							AND 
							NOT ( b.cellidx + b.cellrelay -1 < :cellStart or b.cellidx > :cellEnd ) 
							"
							& weekSQL & 
							"
					) 
				ORDER BY c.b_id, a.cr_floor, a.classroom ";
		
		queryObj = new Query( datasource=application.dnsSlave );
		queryObj.addParam( name="campusId", value=campusId, cfsqltype="cf_sql_char" );
		queryObj.addParam( name="roomType", value=roomType, cfsqltype="cf_sql_char" );
		queryObj.addParam( name="sitsMin", value=roomSize-10, cfsqltype="cf_sql_interger" );
		queryObj.addParam( name="sitsMax", value=roomSize+100, cfsqltype="cf_sql_interger" );
		queryObj.addParam( name="termId", value=termId, cfsqltype="cf_sql_char" );
		queryObj.addParam( name="wkday", value=wkday, cfsqltype="cf_sql_char" );
		queryObj.addParam( name="cellStart", value=cellStart, cfsqltype="cf_sql_interger" );
		queryObj.addParam( name="cellEnd", value=cellEnd, cfsqltype="cf_sql_interger" );

		rs_room = queryObj.execute( sql=sql ).getResult();

		
		/* 查询教学楼 */
		sql = "SELECT DISTINCT 
				c.b_id, c.b_name, c.b_floors, c.campus_id
				FROM t_classroom a 
				INNER JOIN t_building c ON c.b_id = a.b_id
				WHERE 
					a.cr_type = :roomType 
					AND 
					( 
					a.sits > :sitsMin AND a.sits < :sitsMax
					)
					AND 
					substr( a.cr_usage, 4, 1 ) = '1' 
					AND 
					NOT EXISTS (
						SELECT 1 FROM t_clsroom_timetable b
						WHERE 
							a.cr_id = b.cr_id 
							AND 
							b.term_id = :termId 
							AND 
							b.wdidx = :wkday 
							AND 
							NOT ( b.cellidx + b.cellrelay -1 < :cellStart or b.cellidx > :cellEnd )
							"
							& weekSQL & 
							"
					) 
				ORDER BY c.b_name ";
		
		queryObj = new Query( datasource=application.dnsSlave );
		queryObj.addParam( name="roomType", value=roomType, cfsqltype="cf_sql_char" );
		queryObj.addParam( name="sitsMin", value=roomSize-10, cfsqltype="cf_sql_interger" );
		queryObj.addParam( name="sitsMax", value=roomSize+100, cfsqltype="cf_sql_interger" );
		queryObj.addParam( name="termId", value=termId, cfsqltype="cf_sql_char" );
		queryObj.addParam( name="wkday", value=wkday, cfsqltype="cf_sql_char" );
		queryObj.addParam( name="cellStart", value=cellStart, cfsqltype="cf_sql_interger" );
		queryObj.addParam( name="cellEnd", value=cellEnd, cfsqltype="cf_sql_interger" );

		rs_building = queryObj.execute( sql=sql ).getResult();
		
		
		passArgs = structNew();
		structInsert( passArgs, "W1", w1 );
		structInsert( passArgs, "W2", w2 );
		structInsert( passArgs, "WKDAY", wkday );
		structInsert( passArgs, "CELL", wkcell );
		structInsert( passArgs, "CAMP", campusId );
		structInsert( passArgs, "CT", roomType );
		structInsert( passArgs, "SIZE", roomSize );
		
		currentTab = event.getArg("Tab", "labClassroom");
		
	</cfscript>
</cfsilent>

<div id="headArea">
	<div class="uiHeader uiHeaderWithImage ptm">
		<div class="clearfix uiHeaderTop">
			<div class="uiHeaderActions rfloat">
				<a class="uiButton" href="<cfoutput>#buildURL('DEFAULT_EVENT')#</cfoutput>"><i class="mrs img btnList"></i><span class="uiButtonText">教学场地调度</span></a>
			</div>
			<div>
				<h2 class="uiHeaderTitle">
					<i class="uiHeaderImage img icon16x16 info"></i>选择可用教室
				</h2>
			</div>
		</div>
	</div>
</div>

<!--- 加载导航菜单 --->
<!-- stepMenu-->
<cfoutput>#event.getArg("stepMenu")#</cfoutput>
<!--/stepMenu-->

<div class="UIElement">
	<ul class="senateFirstPage">
		<li>
			<div class="clearfix" id="summary">
				
				<div class="UItab2">
					<div class="tabLables">
						<cfif rs_building.recordCount >
							<span <cfif currentTab eq "labBuilding">class="active"</cfif> id="labBuilding" tabTarget="Building">教学区域</span>
						</cfif>
						<span <cfif currentTab eq "labClassroom">class="active"</cfif> id="labClassroom" tabTarget="Classroom">教室</span>
					</div>
				</div>
				<script language="javascript" type="text/javascript">
					//<![CDATA[
					<!--
					$(document).ready(function() {
					
						/* init Tab */
						$("div.tabContent").hide();
						$("div#" + $("div.tabLables span.active").attr("tabTarget")).show();
						
						$("div.tabLables > span").click(function(){
							var $targetID = $(this).attr("tabTarget");
								
							$("div.tabLables span").removeClass("active");
							$(this).addClass("active");
							
							$("div.tabContent").not('[id="' + $targetID + '"]').hide();
							$("div.tabContent#" + $targetID).show();
						});
						
					});
					
					// -->
					//]]>
				</script>
				
				<cfif rs_building.recordCount >
					
					<div id="Building" class="tabContent">
						
						<div class="noticeBlock">
							<div class="operation">
								<a class="uiButton" href="<cfoutput>#buildURL('classRoomBorrow', passArgs)#</cfoutput>"><i class="mrs img btnCampus"></i><span class="uiButtonText">更换使用时段</span></a>
							</div>
							<h3>选择教学区域</h3>
							<p><span class="img icon16x16 message"></span>如果需要指定教室区域, 请在这里进行选择</p>
							<hr/>
						</div>
						
						<div class="clearfix">
							<div class="UICardTable">
								<cfloop query="rs_building">
									<cfset structInsert(passArgs, "BID", rs_building.b_id, true) />
									<cfset structInsert(passArgs, "CAMP", rs_building.campus_id, true) />
									<dl>
										<dt>
											<a href="<cfoutput>#buildURL('classRoomBorrowRoomList', passArgs)#</cfoutput>">
												<em class="building"><!--imgholder--></em>
												<dd>
													<h3><cfoutput>#rs_building.b_name#</cfoutput></h3>
													<div><cfoutput>#dictAdvice.getBuildingFloorSummary(rs_building.b_floors)#</cfoutput></div>
												</dd>
											</a>
										</dt>
									</dl>
											
								</cfloop>
								<!--- 移出楼宇参数 --->
								<cfset structDelete( passArgs, "BID", false ) />
								<!--- 回滚默认值 --->
								<cfset structInsert( passArgs, "CAMP", campusId, true ) />
							</div>
						</div>

					</div>
					
				</cfif>
				
				<div id="Classroom" class="tabContent">
					
					<div class="noticeBlock">
						<div class="operation">
							<a class="uiButton" href="<cfoutput>#buildURL('classRoomBorrow', passArgs)#</cfoutput>"><i class="mrs img btnCampus"></i><span class="uiButtonText">更换使用时段</span></a>
						</div>
						<h3>可用教室清单</h3>
						<p>
							<span class="img icon16x16 message"></span>
							下面是符合教室借用条件的教室清单, 你也可以选择其他的教室类别.
							已选教室 <b id="RoomCount">0</b> 间
						</p>
						<hr/>
					</div>
					
					<cfif rs_room.recordCount >
							
							<table id="roomList" class="UIEditable">
								<thead>
									<tr>
										<td width="30"></td>
										<td>教室</td>
										<td>教学楼</td>
										<td>楼层</td>
										<td width="60" align="left">座位数</td>
										<td width="60" align="left">上课</td>
										<td width="60" align="left">考试</td>
									</tr>
								</thead>
								<tbody>
									<cfloop query="rs_room">
										<tr class="editRows" rowid="<cfoutput>#rs_room.cr_id#</cfoutput>">
											<td class="index"><a rowid="<cfoutput>#rs_room.cr_id#</cfoutput>" class="checkbox stat no" href="javascript:void(0);"></a></td>
											<td><cfoutput>#rs_room.classroom#</cfoutput></td>
											<td><cfoutput>#rs_room.b_name#</cfoutput></td>
											<td><cfoutput>#dictAdvice.getFloorString( rs_room.cr_floor )#</cfoutput></td>
											<td><span class="numeric"><cfoutput>#rs_room.sits#</cfoutput></span></td>
											<td><span class="numeric"><cfoutput>#rs_room.sits_course#</cfoutput></span></td>
											<td><span class="numeric"><cfoutput>#rs_room.sits_exam#</cfoutput></span></td>
										</tr>
									</cfloop>
								</tbody>
							</table>
							
							<script language="javascript" type="text/javascript">
								//<![CDATA[
								<!--
								$(document).ready(function() {
									
									function flushChecked() {
										
										var roomIds = [];
										
										$("#roomList a.checkbox.yes").each(function(index, element) {
											var rowid = $(this).attr("rowid");
											roomIds.push(rowid);
										});

										$("#RoomCount").text( roomIds.length );
										$("#ROOMIDS").val( roomIds.join("|") );
										
										if ( roomIds.length ) {
											$("#formPush").removeClass("hidden_elem");
										}
										else {
											$("#formPush").addClass("hidden_elem");
										}

									}
									
									$("#roomList a.checkbox").click(function(){
									
										var rowid = $(this).attr("rowid");
										var $row = $("tr.editRows[rowid='" + rowid + "']");
										
										var status = $(this).hasClass("yes");
										var newStatus = true;
										
										/* 切换状态数据 */
										if (status) {
											newStatus = false;
										}
										
										if (newStatus) {
											$("a.checkbox", $row).removeClass("no").addClass("yes");
										}
										else {
											$("a.checkbox", $row).removeClass("yes").addClass("no");
										}
										
										flushChecked();
										
									});
									
								});

								// -->
								//]]>
							</script>
							
							<form id="formPush" class="formWrapper Transparent Mini hidden_elem" method="post" action="<cfoutput>#buildURL('classRoomBorrowSign')#</cfoutput>">
								
								<input name="W1" type="hidden" value="<cfoutput>#w1#</cfoutput>" />
								<input name="W2" type="hidden" value="<cfoutput>#w2#</cfoutput>" />
								<input name="WKDAY" type="hidden" value="<cfoutput>#wkday#</cfoutput>" />
								<input name="CAMP" type="hidden" value="<cfoutput>#campusId#</cfoutput>" />
								<input name="CT" type="hidden" value="<cfoutput>#roomType#</cfoutput>" />
								<input name="CELL" type="hidden" value="<cfoutput>#wkcell#</cfoutput>" />
								<input name="SIZE" type="hidden" value="<cfoutput>#roomSize#</cfoutput>" />
								
								<input name="ROOMIDS" id="ROOMIDS" type="hidden" value="" />
								
								<div class="lable">
									<input class="button" type="submit" value="提交申请" />
								</div>
								
							</form>
							
						<cfelse>
							<div class="systemNotice">
								<p>没有适合的教室</p>
							</div>
					</cfif>
					
				</div>
				
			</div>
		</li>
	</ul>
</div>

